package com.park.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.park.model.AuthUser;
import com.park.model.AuthUserRole;
import com.park.model.Monthuser;
import com.park.model.Park;
import com.park.model.Rechargerecord;
import com.park.service.AuthorityService;
import com.park.service.ParkService;
import com.park.service.RechargeRecordService;
import com.park.service.Utility;

@Controller
@RequestMapping("rechargeRecord")
public class RechargeRecordController {
@Autowired
RechargeRecordService rechargeRecordService;
@Autowired
AuthorityService authService;
@Autowired
ParkService parkService;
	
@RequestMapping(value = "/page", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
public @ResponseBody String page(@RequestBody Map<String, Object> args, HttpSession session) {
	@SuppressWarnings("unused")
	int low = (int) args.get("low");
	@SuppressWarnings("unused")
	int count = (int) args.get("count");
	String userName = (String) session.getAttribute("username");
	List<Rechargerecord> list = rechargeRecordService.getByParkAuthority(userName);
	return Utility.createJsonMsg(1001, "success", rechargeRecordService.getByParkAuthority(userName));
}
//充值记录搜索
@RequestMapping(value = "getBySearchRecharge", method = RequestMethod.POST, produces = {
"application/json;charset=UTF-8" })
@ResponseBody
public String getBySearchRecharge(@RequestBody Map<String, String> args, HttpSession session) {
	Map<String, Object> retMap = new HashMap<String, Object>();
	String parkId=args.get("parkId");
	String plateNumber=args.get("plateNumber");
	String parkDesc=args.get("parkDesc");
	String phoneNumber=args.get("phoneNumber");
	String name=args.get("name");
	String username = (String) session.getAttribute("username");
	AuthUser user = authService.getUserByUsername(username);
	if(parkId!=""){
		List<Rechargerecord> rechargerecord=rechargeRecordService.getBySearchRechargeParkId(Integer.parseInt(parkId), parkDesc, plateNumber, phoneNumber, name);
		List<Rechargerecord> recharge=new ArrayList<>();
		if (user.getRole() == AuthUserRole.ADMIN.getValue()){
			recharge=rechargerecord;
		}else {
			List<Park> parkList=parkService.getParks();
			parkList = parkService.filterPark(parkList, username);
			for (Rechargerecord re : rechargerecord) {
				for (Park park : parkList) {
					if (park.getId()==re.getParkid().intValue()) {
						recharge.add(re);
					}
				}
			}
		}
		retMap.put("status", 1001);
		retMap.put("message", "success");
		retMap.put("body", recharge);
	}else if(parkId==""){
		List<Rechargerecord> rechargerecords=rechargeRecordService.getBySearchRecharge(parkDesc, plateNumber, phoneNumber, name);
		List<Rechargerecord> rechargeresult=new ArrayList<>();
		if (user.getRole() == AuthUserRole.ADMIN.getValue()){
			rechargeresult=rechargerecords;
		}else {
			List<Park> parkList=parkService.getParks();
			parkList = parkService.filterPark(parkList, username);
			for (Rechargerecord re : rechargerecords) {
				for (Park park : parkList) {
					if (park.getId()==re.getParkid().intValue()) {
						rechargeresult.add(re);
					}
				}
			}
		}
		retMap.put("status", 1001);
		retMap.put("message", "success");
		retMap.put("body", rechargeresult);
	}else{
		retMap.put("status", 1002);
		retMap.put("message", "faild");
	}
	return Utility.gson.toJson(retMap);
}

}
